<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class GameManager {

	private $db_access;

	public function __construct() {
		$this->db_access = new DbAccessor();
	}

	public function set_db_accessor($db_access) {
		$this->db_access = $db_access;
	}

	public function game_get_games() {
		$sqlresult = $this->get_games_from_db();
		$gs = array();
		while ($g = $this->db_access->db_fetch_array($sqlresult)) {
			if (is_array($gs[$g['game_id']]) and count($gs[$g['game_id']])) {
				$gs[$g['game_id']]['types'][] = $g['game_type'];
			} else {
				$gs[$g['game_id']] = $g;
				$gs[$g['game_id']]['types'] = array($g['game_type']);
			}
		}
		return $gs;
	}

	public function game_get_games_kv() {
		$sqlresult = $this->get_games_from_db();
		$keys = array();
		$values = array();
		while ($g = $this->db_access->db_fetch_array($sqlresult)) {
			if ($prev_type != $g['game_type_id']) {
				$keys[] = "TYPE";
				$values[] = $g['game_type'];
			}
			$keys[] = $g['game_id'];
			$values[] = "&nbsp;&nbsp;&nbsp;&nbsp;{$g['name']}";
			$prev_type = $g['game_type_id'];
		}
		return array('keys' => $keys, 'values' => $values);
	}

	private function get_games_from_db() {
		return $this->db_access->db_do_query($this->db_access->db_query_select(
				array(
						array(), 
						"GAME.*, GAME_TYPE.*"), 
				array(
						"(GAME INNER JOIN GAME_OF_TYPE ON GAME.game_id = GAME_OF_TYPE.game_id) " . 
						"INNER JOIN GAME_TYPE ON GAME_OF_TYPE.game_type_id = GAME_TYPE.game_type_id", 
						"DUMMY"), 
				 1, 
				 "game_type ASC, name ASC"));
	}
}

?>
